﻿using System;
using System.Web.Mvc;

namespace RefLib.Branding.MVC
{
    public static class BrandingHtmlExtensions
    {
        public static MvcHtmlString BrandedImage(this HtmlHelper helper, string resourceId, string tag = null, string subTag = null, object htmlAttributes = null)
        {
            string currentBrand = GetCurrentBrand(helper);
            IBrandingService brandingService = GetBrandingService(helper);
            BlobModel blob = brandingService.GetBrandedBlob(resourceId, tag, subTag, currentBrand);

            if (blob == null)
                throw new Exception("Unable to find blob " + resourceId + " for brand " + currentBrand + ".");

            string actionName = "BrandedImage";
            string controllerName = GetControllerName(helper);
            object routeValues = new { id = blob.BlobGuid };
            string src = UrlAction(helper, actionName, controllerName, routeValues);

            var image = new TagBuilder("img");
            image.MergeAttribute("src", src);
            return new MvcHtmlString(image.ToString());
        }

        public static MvcHtmlString BrandStringValue(this HtmlHelper helper, string resourceId, string tag = null, string subTag = null, string defaultValue = null)
        {
            string currentBrand = GetCurrentBrand(helper);
            IBrandingService brandingService = GetBrandingService(helper);

            return new MvcHtmlString((string) brandingService.GetBrandedValue(resourceId, tag, subTag, currentBrand, defaultValue));
        }

        public static MvcHtmlString BrandText(this HtmlHelper helper, string resourceId, string tag = null, string subTag = null, string defaultValue = null)
        {
            string currentBrand = GetCurrentBrand(helper);
            IBrandingService brandingService = GetBrandingService(helper);

            return new MvcHtmlString(brandingService.GetBrandedText(resourceId, tag, subTag, currentBrand, defaultValue));
        }

        private static IBrandingService GetBrandingService(HtmlHelper helper)
        {
            BrandingController brandingController = helper.ViewContext.Controller as BrandingController;

            if (brandingController == null)
                throw new Exception(helper.ViewContext.Controller.GetType() + " is not derived from BrandingController.");

            IBrandingService brandingService = brandingController.BrandingService;
            if (brandingService == null)
                throw new Exception("BrandingService is null.");

            return brandingService;
        }

        private static string GetControllerName(HtmlHelper helper)
        {
            return (string) helper.ViewContext.RouteData.Values["controller"];
        }

        private static string GetCurrentBrand(HtmlHelper helper)
        {
            string brand = (string) helper.ViewContext.HttpContext.Session["BRAND"];
            return String.IsNullOrEmpty(brand) ? "/" : brand;
        }

        private static string UrlAction(HtmlHelper helper, string actionName, string controllerName, object routeValues)
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            return urlHelper.Action(actionName, controllerName, routeValues);
        }
    }
}
